package format2

import (
	"strconv"
	"strings"
	"regexp"
	"encoding/json"

)

//重点要关注客场和主场的胜率，因为有的球队不擅长客场
/**
[]f1 [总场胜率差,总场平率差,总场负率差]
[]f2 [近场胜率差,近场平率差,近场负率差]
[]f3 [主队主场胜率-客队客场胜率]
[]f4 [总场差占比，近场差占比]
[]f5 [排名差]
 */

func leagueData(text string) (bool,[][]float64) {

	s:=strings.Split(text,"$$$$")

	ll:=strings.Split(s[0],"$$")

	if len(ll)<2||!strings.Contains(ll[0],"^")||!strings.Contains(ll[1],"^"){
		return false,nil
	}

	home:=strings.Split(ll[0],"!")

	guest:=strings.Split(ll[1],"!")
	//总数据
	a1:=strings.Split(home[0],"^")
	a2:=strings.Split(guest[0],"^")

	//近场数据
	b1:=strings.Split(home[3],"^")
	b2:=strings.Split(guest[3],"^")

	//主队主场数据，客队客场数据
	//c1:=strings.Split(home[1],"^")
	//c2:=strings.Split(guest[2],"^")

	//总场数
	n1,_:=strconv.ParseFloat(a1[1],64)
	n2,_:=strconv.ParseFloat(a2[1],64)
	//胜场数
	win1,_:=strconv.ParseFloat(a1[2],64)
	win2,_:=strconv.ParseFloat(a2[2],64)

	//平场数
	fit1,_:=strconv.ParseFloat(a1[3],64)
	fit2,_:=strconv.ParseFloat(a2[3],64)

	//负场数
	loss1,_:=strconv.ParseFloat(a1[4],64)
	loss2,_:=strconv.ParseFloat(a2[4],64)

	//近场胜
	last01,_:=strconv.ParseFloat(b1[2],64)
	last02,_:=strconv.ParseFloat(b2[2],64)
	//近场平
	last11,_:=strconv.ParseFloat(b1[3],64)
	last12,_:=strconv.ParseFloat(b2[3],64)
	//近场负
	last21,_:=strconv.ParseFloat(b1[4],64)
	last22,_:=strconv.ParseFloat(b2[4],64)

	//近场总场数
	t1:=last01+last11+last21
	t2:=last02+last12+last22

	if n1>0&&n2>0{
		//总场胜率差
		f1:=win1/(n1)-win2/(n2)
		//近场胜率差
		f2:=last01/(t1)-last02/(t2)

		//总场平率差
		f3:=fit1/(n1)-fit2/(n2)
		//近场平率差
		f4:=last11/(t1)-last12/(t2)

		//总场负率差
		f5:=loss1/(n1)-loss2/(n2)
		//近场负率差
		f6:=last21/(t1)-last22/(t2)

		//主队主场胜率
		//pc1,_:=strconv.ParseFloat(strings.Replace(c1[9],"%","",-1),64)
		//客队客场胜率
		//pc2,_:=strconv.ParseFloat(strings.Replace(c2[9],"%","",-1),64)

		//排名
		rank1,_:=strconv.ParseFloat(a1[8],64)
		rank2,_:=strconv.ParseFloat(a2[8],64)
		ff1:=[]float64{f1,f3,f5}
		ff2:=[]float64{f2,f4,f6}
		//ff3:=[]float64{(pc1-pc2)/100}
		//ff4:=[]float64{(n1-n2)/(n1+n2),(t1-t2)/(t1+t2)}
		ff5:=[]float64{rank1-rank2}
		return true,[][]float64{ff1,ff2,ff5}
	}
	return false,nil
}
/**
交战数据,最近4场
[]f [胜=1，平=0，负=-1]
 */
func betData(startAt string,t1 string,t2 string,text string) (bool,[]float64) {
	reg, _ := regexp.Compile("\\[[\\S\\s]+?]")
	//主队
	t1= reg.ReplaceAllString(t1, "")
	//客队
	t2= reg.ReplaceAllString(t2, "")
	s:=strings.Split(text,"$$$$")

	m:=strings.Split(s[1],"$$")
	//最近交锋数据处理
	bet:=strings.Split(m[0],"!")
	f:=make([]float64,0)

	i:=0

	for _,v:=range bet{
		if i>9{
			//最近四场
			break
		}
		slice:=strings.Split(v,"^")
		if v==""{
			continue
		}
		/*
		//比赛时间不超过一年
		time1,_:=time.Parse("20060102150405",slice[0])
		time2,_:=time.Parse("06-01-02 15:04",startAt)
		
		if time2.Sub(time1).Hours()>24*365{
			continue
		}
		if time2.Year()-time1.Year()>1{
			continue
		}
		*/
		ta:=slice[2]
		sa,_:=strconv.ParseFloat(slice[4],64)
		sb,_:=strconv.ParseFloat(slice[5],64)

		if strings.Index(ta,t1)>=0{


			if sa>sb{
				f=append(f,1)
			}
			if sa==sb{
				f=append(f,0)
			}
			if sa<sb{
				f=append(f,-1)
			}

		}else{

			if sa<sb{
				f=append(f,1)
			}

			if sa==sb{
				f=append(f,0)
			}
			if sa>sb{
				f=append(f,-1)
			}

		}
		i++

	}
	if len(f)<10{
		size:=10-len(f)

		for k:=0;k<size;k++ {

			f=append(f,-9999)
		}
	}

	return true,f
}

/**
最近10场比赛数据
[]f1 [主队胜,平,负]
[]f2 [客队胜,平,负]
 */
func lastMatchData(league string,t1 string,t2 string,text string) [][]float64 {

	reg, _ := regexp.Compile("\\[[\\S\\s]+?]")
	//主队
	t1= reg.ReplaceAllString(t1, "")
	//客队
	t2= reg.ReplaceAllString(t2, "")
	s := strings.Split(text, "BaseInfo")[1]
	jsonString := "{\"BaseInfo" + s
	data:=make(map[string]interface{})
	json.Unmarshal([]byte(jsonString),&data)
	nearInfo:=data["NearInfo"].(map[string]interface{})
	guestInfo:=nearInfo["GuestInfo"].([]interface{})
	homeInfo:=nearInfo["HomeInfo"].([]interface{})

	f1:=make([]float64,0)
	f2:=make([]float64,0)


	for k,v:=range homeInfo{
		if k>9{
			break
		}
		d:=v.(map[string]interface{})
		isHome:=false
		if strings.Index(d["HomeTeam"].(string),t1)>=0{
			isHome=true
		}
		ff:=d["HomeScore"].(float64)-d["GuestScore"].(float64)
		if (isHome&&ff<0)||(!isHome&&ff>0){
			//输
			f1=append(f1,-1)
		}else if ff==0{
			//平
			f1=append(f1,0)
		}else{
			//赢
			f1=append(f1,1)
		}


	}

	for k,v:=range guestInfo{
		if k>9{
			break
		}
		d:=v.(map[string]interface{})
		isHome:=false
		if strings.Index(d["HomeTeam"].(string),t2)>=0{
			isHome=true
		}
		ff:=d["HomeScore"].(float64)-d["GuestScore"].(float64)
		if (isHome&&ff<0)||(!isHome&&ff>0){
			//输
			f2=append(f2,-1)
		}else if ff==0{
			//平
			f2=append(f2,0)
		}else{
			//赢
			f2=append(f2,1)
		}

	}

	if len(f1)<10{
		size:=10-len(f1)
		for k:=0;k<size;k++ {

			f1=append(f1,-9999)
		}
	}
	if len(f2)<10{
		size:=10-len(f2)
		for k:=0;k<size;k++ {
			f2=append(f2,-9999)
		}
	}
	return [][]float64{
		f1,f2,
	}
}

/**
战绩特征数据，对战前X名、后X名数据
[]f1 [前x名胜率差,前x名平率差,前x名负率差]
[]f2 [后x名胜率差,后x名平率差,后x名负率差]
 */
func combatData(data string) (bool,[][]float64) {
	//前x名胜率差
	f1:=float64(0)
	//前x名平率差
	f2:=float64(0)
	//前x名负率差
	f3:=float64(0)
	//后x名胜率差
	f4:=float64(0)
	//后x名负率差
	f5:=float64(0)
	//后x名平率差
	f6:=float64(0)

	s:=strings.Split(data,"$$$$")
	text:=""
	for _,v:=range s{
		if strings.Contains(v,"listAway"){
			dd:=strings.Split(v,"$$")
			if len(dd)>4{
				text=dd[4]
				if strings.Contains(text,"本赛季"){
					break
				}else{
					text=""
				}
			}
		}
	}
	if text!="" {

		t := strings.Split(text, "!")
		tt1 := strings.Split(t[0], "^")
		tt2 := strings.Split(t[1], "^")
		a1, _ := strconv.ParseFloat(tt1[0], 64)
		a2, _ := strconv.ParseFloat(tt1[1], 64)
		a3, _ := strconv.ParseFloat(tt1[2], 64)

		a4, _ := strconv.ParseFloat(tt1[3], 64)
		a5, _ := strconv.ParseFloat(tt1[4], 64)
		a6, _ := strconv.ParseFloat(tt1[5], 64)

		b1, _ := strconv.ParseFloat(tt2[0], 64)
		b2, _ := strconv.ParseFloat(tt2[1], 64)
		b3, _ := strconv.ParseFloat(tt2[2], 64)

		b4, _ := strconv.ParseFloat(tt2[3], 64)
		b5, _ := strconv.ParseFloat(tt2[4], 64)
		b6, _ := strconv.ParseFloat(tt2[5], 64)

		f1 = a1/(a1+a2+a3) - b1/(b1+b2+b3)
		f2 = a2/(a1+a2+a3) - b2/(b1+b2+b3)
		f3 = a3/(a1+a2+a3) - b3/(b1+b2+b3)

		f4 = a4/(a4+a5+a6) - b4/(b4+b5+b6)
		f5 = a5/(a4+a5+a6) - b5/(b4+b5+b6)
		f6 = a6/(a4+a5+a6) - b6/(b4+b5+b6)

		return true, [][]float64{
			[]float64{f1, f2, f3},
			[]float64{f4, f5, f6},
		}
	}
	return false,nil
}
/**
亚盘赔率数据

 */

func AsiaOdds(data string) []float64 {
	s:=strings.Split(data,"!")

	maxA:=float64(0)
	minA:=float64(9)
	maxB:=float64(0)
	minB:=float64(9)
	totalA:=float64(0)
	totalB:=float64(0)
	count:=float64(0)

	for _,v:=range s {
		ss:=strings.Split(v,"^")
		//只计算0.25盘口的
		if !strings.Contains(ss[6],"0.25"){
			continue
		}
		a,_:=strconv.ParseFloat(ss[5],64)
		b,_:=strconv.ParseFloat(ss[7],64)

		totalA+=a
		totalB+=b
		count++
		if a>=maxA{
			maxA=a
		}
		if a<=minA{
			minA=a
		}
		if b>=maxB{
			maxB=b
		}
		if b<=minB{
			minB=b
		}
	}
	f1:=totalA/count
	f2:=totalB/count
	return []float64{f1,f2,maxA,minA,maxB,minB}
}
func FormatData(id string,league string,startAt string,t1 string,t2 string,text string,asia string)(bool,[][]float64) {
	b1,data1:=leagueData(text)

	if !b1{
		return false,nil
	}
	b2,data2:=betData(startAt,t1,t2,text)

	if !b2{
		return false,nil
	}

	data3:=lastMatchData(league,t1,t2,text)

	temp:=make([]float64,0)
	for _,v:=range data1 {
		for _,v2:=range v {
			temp=append(temp,v2)
		}
	}

	//data4:=AsiaOdds(asia)

	d:=[][]float64{
		temp,
		data2,
		data3[0],data3[1],
	}

	return true,d
}